package com.dawn.anypossible.base.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.boot.configurationprocessor.json.JSONStringer;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.security.web.csrf.DeferredCsrfToken;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * @author: huangxz
 * @Date: 15:41 2024-3-8
 * @Description:
 */
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        Map<String, Object> result = new HashMap<>();
        result.put("code", 401);
        result.put("msg", "未登录");
        result.put("csrf", ((CsrfToken) request.getAttribute("_csrf")).getToken());

        response.setContentType("application/json;charset=UTF-8");
        ObjectMapper objectMapper = new ObjectMapper();
        response.getWriter().write(objectMapper.writeValueAsString(result));

//        CsrfToken csrfToken = d.loadToken(request);
//        System.out.println("dfsdfds" + csrfToken.getToken());

    }
//    d577aabf-e2f1-4c3e-b19c-fcc8a1e8d953
}
